<template>
  <div style="width:80%;margin:0 auto;background-color: #ececec;">
    <h1 style="text-align: center;color: #606266;padding-top: 15px;">器具借用单</h1>
    <div style="width: 80%;margin:0 auto;">
      <el-form ref="form" :model="form" label-width="85px">
        <el-form-item label="编码器具" prop="instrumentsId" @keydown.enter.native="instrumentsId">
          <el-input v-model="form.instrumentsId" style="width: 350px;" ref="instrumentsId" placeholder="请扫描计量器具编码" />
        </el-form-item>
        <el-form-item label="借用人" prop="outPeople" @keydown.enter.native="inPeopleCode">
          <el-input v-model="form.outPeople"  style="width: 350px;margin-right: 10px;" :disabled="inPeopleDisabled" ref="outPeople" placeholder="请扫描借用人二维码" clearable/>
          <el-button type="info" v-if="inPeopleDisabled" @click="reInPeople"  plain >重新录入</el-button>
        </el-form-item>
        <el-form-item label="班组" prop="group">
          <el-input v-model="form.group"  style="width: 350px;margin-right: 10px;" disabled placeholder="扫描借用人二维码自动获取" ref="group" />
        </el-form-item>
        <el-form-item label="库房管理员" prop="lender">
          <el-input v-model="lender"  style="width: 350px;margin-right: 10px;" disabled  ref="lender"  placeholder="库房管理员名称自动获取" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" style="width: 350px;" placeholder="请输入备注" clearable />
        </el-form-item>
        <el-table :data="outList">
          <el-table-column label="器具编码" align="center" prop="instrumentsId" />
          <el-table-column label="器具名称" align="center" prop="name" />
          <el-table-column label="规格" align="center" prop="specification" />
          <el-table-column label="检修时间" align="center" prop="repairTime" />
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top: 20px;padding-bottom: 20px;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="reset">重 置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { addRecords, getRecordsByInstrumentsId, updateRecords } from '@/api/system/records'
import { getPeopleByCode } from '@/api/system/people'
import { getUserProfile } from '@/api/system/user'

export default {
  name: 'form',
  data(){
    return{
      lender:'',
      inPeopleDisabled:false,
      inPeopleDisabled1:false,
      form:{},
      outList:[]
    }
  },
  created() {
    this.getList()
  },
  methods:{
    getList(){
      getUserProfile().then(res=>{
        this.lender = res.data.nickName
      })
    },
    //扫描物料编码
    instrumentsId(e){
      let instrumentsId = e.target.value;
      let isAdd = false;
      this.outList.forEach((item)=>{
        if (instrumentsId === item.instrumentsId){
          this.$modal.msgError("该计量器具已扫过码！");
          this.form.instrumentsId = null;
          isAdd = true;
        }
      })
      if(isAdd){
        return;
      }
        getRecordsByInstrumentsId(instrumentsId).then(response=>{
          if(response.data !== undefined){
            if(response.data.inventory === 1){
              this.$modal.msgError("该计量器具已被借用,请先归还！")
              return;
            }
            this.outList.push(response.data);
          }else {
            this.$modal.msgError("没有此物料,请在基础数据库入库！")
          }
        })
      this.form.instrumentsId = null;
      // console.log(this.goodsInList);
    },
    //重新录入借用人员
    reInPeople(){
      this.inPeopleDisabled = false
      this.form.outPeople = null
      this.form.group = null
    },
    //重新录入归还人员
    reInPeople1(){
      this.inPeopleDisabled1 = false
      this.form.returnPeople = null
    },
    //扫描入库人员二维码
    inPeopleCode(e){
      let value = e.target.value;
      if (value === ''){
        return;
      }
      getPeopleByCode(value).then(response=>{
        if(response.data !== undefined){
          this.form.outPeople = response.data.name
          this.form.group = response.data.group
          this.inPeopleDisabled = true
        }else {
          this.$modal.msgError("没有此人员,请在人员基础数据库添加！")
        }
      })
    },
    //扫描归还人员二维码
    returnPeopleCode(e){
      let value = e.target.value;
      if (value === ''){
        return;
      }
      getPeopleByCode(value).then(response=>{
        if(response.data !== undefined){
          this.form.returnPeople = response.data.name
          this.inPeopleDisabled1 = true
        }else {
          this.$modal.msgError("没有此人员,请在人员基础数据库添加！")
        }
      })
    },
    // 表单重置
    reset() {
      this.inPeopleDisabled = false
      this.inPeopleDisabled1 = false
      this.outList = [];
      this.form = {
        id: null,
        instrumentsId: null,
        name: null,
        specification: null,
        outTime: null,
        outPeople: null,
        returnPeople:null,
        outNum: null,
        isReturn: null,
        returnTime: null,
        group:null,
        note:null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if(this.form.returnPeople === null || this.form.returnPeople === undefined || this.form.returnPeople === ''){
              this.$modal.msgError('归还人员不能为空哦!')
              return;
            }
            if(this.form.returnPeople !== this.form.outPeople){
              this.$modal.msgError('借用人员与归还人员不一致!')
              return;
            }
            //记录归还时间
            let goodsInTime = new Date()
            updateRecords(this.form).then(response => {
              this.$modal.msgSuccess("归还成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (this.outList.length === 0){
              this.$modal.msgError('器具借用单不能为空哦!')
              return;
            }
            if(this.form.outPeople === undefined || this.form.outPeople === null || this.form.outPeople === ''){
              this.$modal.msgError('借用人员不能为空哦!')
              return;
            }
            //记录出库时间
            let goodsInTime = new Date()
            // console.log(parseTime(goodsInTime, '{y}-{m}-{d} {h}:{i}'))
            // console.log(goodsInTime)
            this.outList.forEach((item)=>{
              item.outTime = goodsInTime;
              item.outPeople = this.form.outPeople;
              item.group = this.form.group;
              item.isReturn = 0;
              item.lender = this.lender;
              item.note = this.form.note;
            })
            addRecords(this.outList).then(response => {
              this.$modal.msgSuccess("借用成功");
              this.open = false;
              this.reset()
              this.$router.push('/instruments/not_return')
            });
          }
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
